﻿using System;
using System.Collections.Generic;
using UpdaterComponents.Common;
using UpdaterComponents.Tasks;

namespace UpdaterApp
{
    public class QueProcessor
    {
        #region Delegates

        public delegate void IndicateTaskhandler(string msg);

        public delegate void UpgradeCompletedHandler(bool bStatus);

        public delegate void WorkProgresshandler(float fVal);

        #endregion

        private static QueProcessor m_QueProcessor;
        private static Queue<ITask> m_lstTasks;

        private QueProcessor()
        {
            m_lstTasks = new Queue<ITask>();
            AddTasks();
        }

        public static Queue<ITask> TasksList
        {
            get { return m_lstTasks; }
            set { m_lstTasks = value; }
        }

        public event WorkProgresshandler WorkProgress;

        public event IndicateTaskhandler IndicateTask;

        public event UpgradeCompletedHandler OnUpgradeCompleted;


        public static QueProcessor GetInstance()
        {
            if (m_QueProcessor == null)
            {
                m_QueProcessor = new QueProcessor();
            }
            return m_QueProcessor;
        }

        private void AddTasks()
        {
            BackUpCreator backupCreator = new BackUpCreator();
            m_lstTasks.Enqueue(backupCreator);

            UpdateDownloader updateDownloader = new UpdateDownloader();
            m_lstTasks.Enqueue(updateDownloader);

            
            DecompressPackage deCompressPackage = new DecompressPackage();
            m_lstTasks.Enqueue(deCompressPackage);
            //TempRemover tempRemover = new TempRemover();
            //m_lstTasks.Enqueue(tempRemover);

            UpdateInstaller updateInstaller = new UpdateInstaller();
            m_lstTasks.Enqueue(updateInstaller);

        }


        public  void ProcessQue()
        {

            Queue<ITask> tempQue = new Queue<ITask>(m_lstTasks);

            int iTotalTasks = tempQue.Count;

            float fPercentagePerTask = 100/tempQue.Count;
            float finitPercentage = 0.0f;
            bool bIsThereAnyError = false;
            foreach (ITask task in tempQue)
            {

                try
                {
                    IndicateTask(task.Message);
                    task.DoTask();
                    m_lstTasks.Dequeue();
                    finitPercentage = finitPercentage + fPercentagePerTask;


                    //For all the tasks increment percentage task wise
                    //For last task make the progress 100percent
                    if (m_lstTasks.Count != 0)
                        WorkProgress(finitPercentage);
                    else
                        WorkProgress(100.0F);
                }
                catch (Exception ex)
                {
                    bIsThereAnyError = true;
                    task.RollBack();
                    OnUpgradeCompleted(bIsThereAnyError);
                    break;
                }
            }

            if (!bIsThereAnyError)
            {
                OnUpgradeCompleted(bIsThereAnyError);
            }
           
        }

    }
}
